<?php

class Admin_Service_Controller extends Base_Controller {

    public function action_index()
    {   
        $m = Input::get('m',null);
        $k = Input::get('k',null);
        $data['sk.mark']=array('like','%'.$k."%");
        $data['sk.svname']=array('like','%'.$k."%");
        $data['sk.content']=array('like','%'.$k."%");
        $data['sm.shopname']=array('like','%'.$k."%");
        $area['sk.mark'] = array('=',$m);
        if($m){
            $str = DB::table('service_kind as sk')
            ->left_join('shop_management as sm','sk.mark','=','sm.mark')
            ->where('sk.dele','=',1)
            ->where(function($query) use($area) {
            foreach($area as $column => $val)
            {
              $query->where($column, $val[0], $val[1]);
            }
            })
            ->where(function($query) use($data) {
            foreach($data as $column => $val)
            {
              $query->or_where($column, $val[0], $val[1]);
            }
            })
            ->paginate(10);
        return View::make('admin/service',array('res'=>$str,'m'=>$m,'k'=>$k));

        }else{
            $str = DB::table('service_kind as sk')
                ->left_join('shop_management as sm','sk.mark','=','sm.mark')
                ->where('sk.dele','=',1)
                ->where(function($query) use($data) {
                foreach($data as $column => $val)
                {
                  $query->or_where($column, $val[0], $val[1]);
                }
                })
                ->order_by('sk.pid','asc')
                 ->paginate(10);
            return View::make('admin/service',array('res'=>$str,'k'=>$k));
        }
        

        
    }


    public function action_dele()
    {
        $pid = Input::get('pid');
        $dele = 0;
        $str = DB::table('service_kind')->where('pid','=',$pid)->update(array('dele'=>$dele));
        if($str){
            $data = array(
                'code'=>'200',
                'msg'=>'删除成功'
                );
        }else{
            $data = array(
                'code'=>'400',
                'msg'=>'删除失败'
                );
        }
        return json_encode($data);

    }



    public function action_change()
    {

        $pid = Input::get('pid');
        $res = DB::table('service_kind')->where('pid','=',$pid)->first();
        // var_dump($res);
        // exit;
        return View::make('admin/alertservicechange',array('str'=>$res));


    }


    public function action_dochange()
    {

        $pid = Input::get('id');
        $svname = Input::get('svname');
        $content = Input::get('content');
        $price1 = Input::get('price');
        $price = substr($price1,2);
        $str = DB::table('service_kind')->where('pid','=',$pid)->update(array('svname'=>$svname,'content'=>$content,'price'=>$price));
        if($str){
            $data = array(
                'code'=>'20',
                'msg'=>'修改成功'
                );
        }else{
            $data = array(
                'code'=>'-20',
                'msg'=>'修改失败'
                );
        }
        return json_encode($data);

    }


    public function action_addservice()
    {

        $pid = Input::get('pid');
        if($pid){
            $str = DB::table('service_kind')->where('pid','=',$pid)->first();
            $mark = $str['mark'];
            return View::make('admin/addservice',array('mark'=>$mark));
        }else{
            return View::make('admin/addservice');
        }
        
}

    public function action_doaddsrevice()
    {
        $mark = Input::get('mark');
        $svname = Input::get('svname');
        $content = Input::get('content');
        $price = Input::get('price');
        $str = DB::table('service_kind')->insert(array('svname'=>$svname,'content'=>$content,'mark'=>$mark,'price'=>$price));
        if($str){
            return View::make('jump/jumpto',array('msg'=>'添加成功','url'=>'index','time'=>2));
        }else{
            return View::make('jump/error',array('msg'=>'添加失败','time'=>5));
        }

    }





































}